
package com.eworking.eim.encrypt;

import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.eworking.eim.util.EIMLog;

import android.util.Base64;

public class EIMCryptoUtils {
    
    /** 日志输出标记 */
    private static final String _TAG = EIMCryptoUtils.class.getSimpleName();
    
    Cipher cipher = null;
    
    Cipher decipher = null;
    
    /** DES算法 */
    public static final int _ALGORIGHM_DES = 0;
    
    /** AES算法 */
    public static final int _ALGORIGHM_AES = 1;
    
    private byte[] keyBytes = { 74, 111, 104, 110, 115, 111, 110, 77, 97, 74, 105, 70, 97, 110, 103, 74, 101, 114, 118,
        105, 115, 76, 105, 117, 76, 105, 117, 83, 104, 97, 111, 90 };
    
    private String key = "TongliforniaJohnson";
    
    private static final String HEXES = "0123456789ABCDEF";
    
    private static EIMCryptoUtils instance = null;
    
    public static EIMCryptoUtils getInstance() {
        if (instance == null) {
            instance = new EIMCryptoUtils();
        }
        return instance;
    }
    
    public void init(int type) {
        if (type == 0)
            initDES();
        else
            initAES();
    }
    
    public void initDES() {
        MessageDigest localMessageDigest;
        try {
            localMessageDigest = MessageDigest.getInstance("md5");
            byte[] arrayOfByte = localMessageDigest.digest(this.key.getBytes("utf-8"));
            this.keyBytes = Arrays.copyOf(arrayOfByte, 24);
            int i = 0;
            int j = 16;
            while (i < 8)
                this.keyBytes[(j++)] = this.keyBytes[(i++)];
            SecretKeySpec localSecretKeySpec = new SecretKeySpec(this.keyBytes, "DESede");
            IvParameterSpec localIvParameterSpec = new IvParameterSpec(new byte[8]);
            this.cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            this.cipher.init(1, localSecretKeySpec, localIvParameterSpec);
            this.decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            this.decipher.init(2, localSecretKeySpec, localIvParameterSpec);
            EIMLog.d("encrypt", "initital for DES");
        } catch (Exception e) {
            EIMLog.d(_TAG, "初始化DES加密信息失败," + e.getMessage());
        }
    }
    
    public void initAES() {
        try {
            this.cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec localSecretKeySpec = new SecretKeySpec(this.keyBytes, "AES");
            this.cipher.init(1, localSecretKeySpec);
            this.decipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            this.decipher.init(2, localSecretKeySpec);
            EIMLog.d("encrypt", "initital for AES");
        } catch (Exception e) {
            EIMLog.d(_TAG, "初始化AES加密信息失败," + e.getMessage());
        }
    }
    
    public String encryptBase64String(String paramString) throws Exception {
        byte[] arrayOfByte = encrypt(paramString);
        return new String(Base64.encode(arrayOfByte, 0));
    }
    
    public String decryptBase64String(String paramString) throws Exception {
        byte[] arrayOfByte1 = Base64.decode(paramString, 0);
        byte[] arrayOfByte2 = decrypt(arrayOfByte1);
        return new String(arrayOfByte2, "UTF-8");
    }
    
    public byte[] encrypt(String paramString) throws Exception {
        byte[] arrayOfByte1 = paramString.getBytes("UTF-8");
        byte[] arrayOfByte2 = this.cipher.doFinal(arrayOfByte1);
        return arrayOfByte2;
    }
    
    public byte[] encrypt(byte[] paramArrayOfByte) throws Exception {
        return this.cipher.doFinal(paramArrayOfByte);
    }
    
    public byte[] decrypt(byte[] paramArrayOfByte) throws Exception {
        return this.decipher.doFinal(paramArrayOfByte);
    }
    
    public static String getHex(byte[] paramArrayOfByte) {
        byte[] arrayOfByte;
        if (paramArrayOfByte == null)
            return null;
        StringBuilder localStringBuilder = new StringBuilder(2 * paramArrayOfByte.length);
        int k = (arrayOfByte = paramArrayOfByte).length;
        for (int j = 0; j < k; ++j) {
            int i = arrayOfByte[j];
            localStringBuilder.append(HEXES.charAt((i & 0xF0) >> 4)).append(HEXES.charAt(i & 0xF));
        }
        return localStringBuilder.toString();
    }
}
